﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SQLite;

namespace InterValometer
{
    class SessionDB
    {
        String dbString;
        SQLiteConnection dbconn;
        public Acquisition Acquisition;
        public Location Location;
        public Target Target;
        public Sequence Sequence;

        /// <summary>
        /// 
        /// </summary>
        public SessionDB()
        {
            // Reset fields to sane defaults
            this.Clear();
        }

        /// <summary>
        /// Reset fields to sane defaults
        /// </summary>
        /// <returns></returns>
        public void Clear()
        {
            this.UID = -1;
            Acquisition = new Acquisition();
            Location = new Location();
            Target = new Target();
            Sequence = new Sequence();
        }

        /// <summary>
        /// Initialize a new database and populate with sample entries
        /// </summary>
        /// <returns></returns>
        public bool Initialize()
        {
            // Open database or initialize it if it doesn't exist
            dbString = "Data Source=intervalometer.db";

            if (File.Exists("intervalometer.db"))
            {
                dbconn = new SQLiteConnection(dbString);
                dbconn.Open();
            }
            else
            {
                SQLiteConnection.CreateFile("intervalometer.db");
                dbconn = new SQLiteConnection(dbString);
                dbconn.Open();

                // Create database and populate with sample data
                using (SQLiteCommand mycom = new SQLiteCommand(dbconn))
                {
                    mycom.CommandText = @"BEGIN TRANSACTION;
                    CREATE TABLE Acquisition (ACQ_UID INTEGER PRIMARY KEY, ACQ_NAME TEXT, ACQ_DATE TEXT, ACQ_TIME TEXT, ACQ_TEMP TEXT);
                    INSERT INTO Acquisition VALUES(1,'Test Acquisition','10/13/2013','4:10:08 PM',78);
                    INSERT INTO Acquisition VALUES(2,'Test Acquisition 3','10/06/2012','14:10:08 PM',63);
                    CREATE TABLE Location (LOC_UID INTEGER PRIMARY KEY, LOC_NAM TEXT, LOC_LAT_DEG TEXT, LOC_LAT_MIN TEXT, LOC_LAT_SEC TEXT, LOC_LAT_HEM TEXT, LOC_LON_DEG TEXT, LOC_LON_MIN TEXT, LOC_LON_SEC TEXT, LOC_LON_HEM TEXT);
                    INSERT INTO Location VALUES(1,'Staunton River State Park',36,41,58.45,'N',-78,39,50.57,'W');
                    INSERT INTO Location VALUES(2,'Medoc Mountain State Park',36,41,58.45,'N',-78,39,50.57,'W');
                    CREATE TABLE Sequence (SEQ_UID INTEGER PRIMARY KEY, SEQ_PRE_DEL TEXT, SEQ_EXP_DUR TEXT, SEQ_NUM_EXP TEXT, SEQ_FRM_DEL TEXT, SEQ_ISO_SPD TEXT, SEQ_BLB_MOD TEXT);
                    INSERT INTO Sequence VALUES(1,5,20,30,9,1600,1);
                    INSERT INTO Sequence VALUES(2,5,120,30,9,3200,1);
                    CREATE TABLE Session (SES_UID INTEGER PRIMARY KEY, SES_ACQ_UID NUMERIC, SES_LOC_UID NUMERIC, SES_TAR_UID NUMERIC, SES_SEQ_UID NUMERIC);
                    INSERT INTO Session VALUES(1,1,1,1,1);
                    INSERT INTO Session VALUES(2,2,2,2,2);
                    CREATE TABLE Target (TAR_UID INTEGER PRIMARY KEY, TAR_NAM TEXT, TAR_RA_HRS TEXT, TAR_RA_MIN TEXT, TAR_RA_SEC TEXT, TAR_DEC_DEG TEXT, TAR_DEC_MIN TEXT, TAR_DEC_SEC TEXT);
                    INSERT INTO Target VALUES(1,'M 013',16,41,42.0,36,28,0);
                    INSERT INTO Target VALUES(2,'M 042',16,41,42.0,36,28,0);
                    CREATE UNIQUE INDEX ACQ_UID_IDX ON Acquisition(ACQ_UID ASC);
                    CREATE UNIQUE INDEX LOC_UID_IDX ON Location(LOC_UID ASC);
                    CREATE UNIQUE INDEX SEQ_UID_IDX ON Sequence(SEQ_UID ASC);
                    CREATE UNIQUE INDEX SES_UID_IDX ON Session(SES_UID ASC);
                    CREATE UNIQUE INDEX TAR_UID_IDX ON Target(TAR_UID ASC);
                    COMMIT;";

                    mycom.ExecuteNonQuery();
                }
            }

            return true;
        }

        /// <summary>
        /// Close database
        /// </summary>
        public void Close()
        {
            dbconn.Close();
        }

        /// <summary>
        /// Select all entries from session table
        /// </summary>
        /// <returns></returns>
        public DataTable SelectAll()
        {
            DataTable dt = new DataTable();

            using (SQLiteCommand mycom = new SQLiteCommand(dbconn))
            {
                try
                {
                    mycom.CommandText = @"SELECT SES_UID,Acquisition.ACQ_NAME,Acquisition.ACQ_DATE FROM Session,Acquisition WHERE
                                          Session.SES_ACQ_UID=Acquisition.ACQ_UID ORDER BY Acquisition.ACQ_DATE;";
                    SQLiteDataReader rdr = mycom.ExecuteReader();
                    dt.Load(rdr);
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
            }

            return dt;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public bool Select(string ID)
        {
            this.UID = Convert.ToInt32(ID);

            using (SQLiteCommand mycom = new SQLiteCommand(dbconn))
            {
                // Make sure we're not dealing with a new unsaved entry
                if (this.UID != -1)
                {
                    try
                    {
                        mycom.CommandText = @"SELECT Acquisition.*,Location.*,Target.*,Sequence.* FROM Session,Acquisition,Location,Target,Sequence WHERE
                                          ses_uid=@ID AND ses_seq_uid=seq_uid AND ses_tar_uid=tar_uid AND ses_loc_uid=loc_uid AND ses_acq_uid=acq_uid;";
                        mycom.Parameters.AddWithValue("@ID", ID);

                        SQLiteDataReader rdr = mycom.ExecuteReader();

                        // populate all data structures from the database
                        if (rdr.Read())
                        {
                            this.Acquisition.UID = Convert.ToInt32(rdr[0].ToString());
                            this.Acquisition.Name = rdr[1].ToString();
                            this.Acquisition.Date = Convert.ToDateTime(rdr[2].ToString());
                            this.Acquisition.Time = Convert.ToDateTime(rdr[3].ToString());
                            this.Acquisition.Temperature = Convert.ToInt32(rdr[4].ToString());

                            this.Location.UID = Convert.ToInt32(rdr[5].ToString());
                            this.Location.Name = rdr[6].ToString();
                            this.Location.LatitudeDegrees = Convert.ToInt32(rdr[7].ToString());
                            this.Location.LatitudeMinutes = Convert.ToInt32(rdr[8].ToString());
                            this.Location.LatitudeSeconds = Convert.ToDecimal(rdr[9].ToString());
                            this.Location.LongitudeDegrees = Convert.ToInt32(rdr[11].ToString());
                            this.Location.LongitudeMinutes = Convert.ToInt32(rdr[12].ToString());
                            this.Location.LongitudeSeconds = Convert.ToDecimal(rdr[13].ToString());

                            this.Target.UID = Convert.ToInt32(rdr[15].ToString());
                            this.Target.Name = rdr[16].ToString();
                            this.Target.RightAscensionHours = Convert.ToInt32(rdr[17].ToString());
                            this.Target.RightAscensionMinutes = Convert.ToInt32(rdr[18].ToString());
                            this.Target.RightAscensionSeconds = Convert.ToDecimal(rdr[19].ToString());
                            this.Target.DeclinationDegrees = Convert.ToInt32(rdr[20].ToString());
                            this.Target.DeclinationMinutes = Convert.ToInt32(rdr[21].ToString());
                            this.Target.DeclinationSeconds = Convert.ToDecimal(rdr[22].ToString());

                            this.Sequence.UID = Convert.ToInt32(rdr[23].ToString());
                            this.Sequence.PreDelay = Convert.ToInt32(rdr[24].ToString());
                            this.Sequence.ExposureDuration = Convert.ToInt32(rdr[25].ToString());
                            this.Sequence.NumberOfExposures = Convert.ToInt32(rdr[26].ToString());
                            this.Sequence.FrameDelay = Convert.ToInt32(rdr[27].ToString());
                            this.Sequence.ISOSpeed = rdr[28].ToString();
                            this.Sequence.BulbMode = Convert.ToInt32(rdr[29].ToString());
                        }
                    }
                    catch (Exception e)
                    {
                        throw new Exception(e.Message);
                    }
                }
                else
                {
                }
             }   

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool Save()
        {
            using (SQLiteTransaction mytran = dbconn.BeginTransaction())
            {
                if (this.UID != -1)
                {
                    // dealing with an existing entry (UID != -1) so perform UPDATE
                    using (SQLiteCommand mycom = new SQLiteCommand(dbconn))
                    {
                        string sql = @"UPDATE Acquisition SET ACQ_NAME=@ACQ_NAME, ACQ_DATE=@ACQ_DATE, ACQ_TIME=@ACQ_TIME,
                                   ACQ_TEMP=@ACQ_TEMP WHERE ACQ_UID=@ACQ_UID";

                        mycom.CommandText = sql;
                        mycom.Parameters.AddWithValue("@ACQ_UID", this.Acquisition.UID);
                        mycom.Parameters.AddWithValue("@ACQ_NAME", this.Acquisition.Name);
                        mycom.Parameters.AddWithValue("@ACQ_DATE", this.Acquisition.Date);
                        mycom.Parameters.AddWithValue("@ACQ_TIME", this.Acquisition.Time);
                        mycom.Parameters.AddWithValue("@ACQ_TEMP", this.Acquisition.Temperature);
                        //System.Diagnostics.Debug.WriteLine(Acquisition);
                        mycom.ExecuteNonQuery();

                        sql = @"UPDATE Location SET LOC_NAM=@LOC_NAM, LOC_LAT_DEG=@LOC_LAT_DEG, LOC_LAT_MIN=@LOC_LAT_MIN,
                            LOC_LAT_SEC=@LOC_LAT_SEC, LOC_LON_DEG=@LOC_LON_DEG, LOC_LON_MIN=@LOC_LON_MIN, LOC_LON_SEC=@LOC_LON_SEC
                            WHERE LOC_UID=@LOC_UID;";

                        mycom.CommandText = sql;
                        mycom.Parameters.Clear();
                        mycom.Parameters.AddWithValue("@LOC_UID", this.Location.UID);
                        mycom.Parameters.AddWithValue("@LOC_NAM", this.Location.Name);
                        mycom.Parameters.AddWithValue("@LOC_LAT_DEG", this.Location.LatitudeDegrees);
                        mycom.Parameters.AddWithValue("@LOC_LAT_MIN", this.Location.LatitudeMinutes);
                        mycom.Parameters.AddWithValue("@LOC_LAT_SEC", this.Location.LatitudeSeconds);
                        mycom.Parameters.AddWithValue("@LOC_LON_DEG", this.Location.LongitudeDegrees);
                        mycom.Parameters.AddWithValue("@LOC_LON_MIN", this.Location.LongitudeMinutes);
                        mycom.Parameters.AddWithValue("@LOC_LON_SEC", this.Location.LongitudeSeconds);
                        //System.Diagnostics.Debug.WriteLine(Location);
                        mycom.ExecuteNonQuery();

                        sql = @"UPDATE Target SET TAR_NAM=@TAR_NAM, TAR_RA_HRS=@TAR_RA_HRS, TAR_RA_MIN=@TAR_RA_MIN, TAR_RA_SEC=@TAR_RA_SEC,
                            TAR_DEC_DEG=@TAR_DEC_DEG, TAR_DEC_MIN=@TAR_DEC_MIN, TAR_DEC_SEC=@TAR_DEC_SEC WHERE TAR_UID=@TAR_UID;";

                        mycom.CommandText = sql;
                        mycom.Parameters.Clear();
                        mycom.Parameters.AddWithValue("@TAR_UID", this.Target.UID);
                        mycom.Parameters.AddWithValue("@TAR_NAM", this.Target.Name);
                        mycom.Parameters.AddWithValue("@TAR_RA_HRS", this.Target.RightAscensionHours);
                        mycom.Parameters.AddWithValue("@TAR_RA_MIN", this.Target.RightAscensionMinutes);
                        mycom.Parameters.AddWithValue("@TAR_RA_SEC", this.Target.RightAscensionSeconds);
                        mycom.Parameters.AddWithValue("@TAR_DEC_DEG", this.Target.DeclinationDegrees);
                        mycom.Parameters.AddWithValue("@TAR_DEC_MIN", this.Target.DeclinationMinutes);
                        mycom.Parameters.AddWithValue("@TAR_DEC_SEC", this.Target.DeclinationSeconds);
                        //System.Diagnostics.Debug.WriteLine(Target);
                        mycom.ExecuteNonQuery();

                        sql = @"UPDATE Sequence SET SEQ_PRE_DEL=@SEQ_PRE_DEL, SEQ_EXP_DUR=@SEQ_EXP_DUR, SEQ_NUM_EXP=@SEQ_NUM_EXP,
                            SEQ_FRM_DEL=@SEQ_FRM_DEL, SEQ_ISO_SPD=@SEQ_ISO_SPD, SEQ_BLB_MOD=@SEQ_BLB_MOD WHERE SEQ_UID=@SEQ_UID;";

                        mycom.CommandText = sql;
                        mycom.Parameters.Clear();
                        mycom.Parameters.AddWithValue("@seq_UID", this.Sequence.UID);
                        mycom.Parameters.AddWithValue("@SEQ_PRE_DEL", this.Sequence.PreDelay);
                        mycom.Parameters.AddWithValue("@SEQ_EXP_DUR", this.Sequence.ExposureDuration);
                        mycom.Parameters.AddWithValue("@SEQ_NUM_EXP", this.Sequence.NumberOfExposures);
                        mycom.Parameters.AddWithValue("@SEQ_FRM_DEL", this.Sequence.FrameDelay);
                        mycom.Parameters.AddWithValue("@SEQ_ISO_SPD", this.Sequence.ISOSpeed);
                        mycom.Parameters.AddWithValue("@SEQ_BLB_MOD", this.Sequence.BulbMode);
                        //System.Diagnostics.Debug.WriteLine(Sequence);
                        mycom.ExecuteNonQuery();
                    }
                }
                else
                {
                    // dealing with a new entry (UID=-1) so perform INSERT
                    using (SQLiteCommand mycom = new SQLiteCommand(dbconn))
                    {
                        string sql = @"INSERT INTO Acquisition (ACQ_UID, ACQ_NAME, ACQ_DATE, ACQ_TIME, ACQ_TEMP)
                                       VALUES (NULL, @ACQ_NAME, @ACQ_DATE, @ACQ_TIME, @ACQ_TEMP)";

                        mycom.CommandText = sql;
                        mycom.Parameters.AddWithValue("@ACQ_NAME", this.Acquisition.Name);
                        mycom.Parameters.AddWithValue("@ACQ_DATE", this.Acquisition.Date);
                        mycom.Parameters.AddWithValue("@ACQ_TIME", this.Acquisition.Time);
                        mycom.Parameters.AddWithValue("@ACQ_TEMP", this.Acquisition.Temperature);
                        //System.Diagnostics.Debug.WriteLine(Acquisition);
                        mycom.ExecuteNonQuery();

                        // get auto-generated primary key of last row inserted
                        mycom.CommandText = "SELECT last_insert_rowid()";
                        Int64 tmp_uid = (Int64)mycom.ExecuteScalar();
                        this.Acquisition.UID = (int)tmp_uid;

                        sql = @"INSERT INTO Location (LOC_UID, LOC_NAM, LOC_LAT_DEG, LOC_LAT_MIN, LOC_LAT_SEC, LOC_LON_DEG, LOC_LON_MIN, LOC_LON_SEC)
                                VALUES (NULL, @LOC_NAM, @LOC_LAT_DEG, @LOC_LAT_MIN, @LOC_LAT_SEC, @LOC_LON_DEG, @LOC_LON_MIN, @LOC_LON_SEC);";

                        mycom.CommandText = sql;
                        mycom.Parameters.Clear();
                        mycom.Parameters.AddWithValue("@LOC_NAM", this.Location.Name);
                        mycom.Parameters.AddWithValue("@LOC_LAT_DEG", this.Location.LatitudeDegrees);
                        mycom.Parameters.AddWithValue("@LOC_LAT_MIN", this.Location.LatitudeMinutes);
                        mycom.Parameters.AddWithValue("@LOC_LAT_SEC", this.Location.LatitudeSeconds);
                        mycom.Parameters.AddWithValue("@LOC_LON_DEG", this.Location.LongitudeDegrees);
                        mycom.Parameters.AddWithValue("@LOC_LON_MIN", this.Location.LongitudeMinutes);
                        mycom.Parameters.AddWithValue("@LOC_LON_SEC", this.Location.LongitudeSeconds);
                        //System.Diagnostics.Debug.WriteLine(Location);
                        mycom.ExecuteNonQuery();

                        // get auto-generated primary key of last row inserted
                        mycom.CommandText = "SELECT last_insert_rowid()";
                        tmp_uid = (Int64)mycom.ExecuteScalar();
                        this.Location.UID = (int)tmp_uid;

                        sql = @"INSERT INTO Target (TAR_UID, TAR_NAM, TAR_RA_HRS, TAR_RA_MIN, TAR_RA_SEC, TAR_DEC_DEG, TAR_DEC_MIN, TAR_DEC_SEC)
                                VALUES (NULL, @TAR_NAM, @TAR_RA_HRS, @TAR_RA_MIN, @TAR_RA_SEC, @TAR_DEC_DEG, @TAR_DEC_MIN, @TAR_DEC_SEC);";

                        mycom.CommandText = sql;
                        mycom.Parameters.Clear();
                        mycom.Parameters.AddWithValue("@TAR_NAM", this.Target.Name);
                        mycom.Parameters.AddWithValue("@TAR_RA_HRS", this.Target.RightAscensionHours);
                        mycom.Parameters.AddWithValue("@TAR_RA_MIN", this.Target.RightAscensionMinutes);
                        mycom.Parameters.AddWithValue("@TAR_RA_SEC", this.Target.RightAscensionSeconds);
                        mycom.Parameters.AddWithValue("@TAR_DEC_DEG", this.Target.DeclinationDegrees);
                        mycom.Parameters.AddWithValue("@TAR_DEC_MIN", this.Target.DeclinationMinutes);
                        mycom.Parameters.AddWithValue("@TAR_DEC_SEC", this.Target.DeclinationSeconds);
                        //System.Diagnostics.Debug.WriteLine(Target);
                        mycom.ExecuteNonQuery();

                        // get auto-generated primary key of last row inserted
                        mycom.CommandText = "SELECT last_insert_rowid()";
                        tmp_uid = (Int64)mycom.ExecuteScalar();
                        this.Target.UID = (int)tmp_uid;

                        sql = @"INSERT INTO Sequence (SEQ_UID, SEQ_PRE_DEL, SEQ_EXP_DUR, SEQ_NUM_EXP, SEQ_FRM_DEL, SEQ_ISO_SPD, SEQ_BLB_MOD)
                                VALUES (NULL, @SEQ_PRE_DEL, @SEQ_EXP_DUR, @SEQ_NUM_EXP, @SEQ_FRM_DEL, @SEQ_ISO_SPD, @SEQ_BLB_MOD);";

                        mycom.CommandText = sql;
                        mycom.Parameters.Clear();
                        mycom.Parameters.AddWithValue("@SEQ_PRE_DEL", this.Sequence.PreDelay);
                        mycom.Parameters.AddWithValue("@SEQ_EXP_DUR", this.Sequence.ExposureDuration);
                        mycom.Parameters.AddWithValue("@SEQ_NUM_EXP", this.Sequence.NumberOfExposures);
                        mycom.Parameters.AddWithValue("@SEQ_FRM_DEL", this.Sequence.FrameDelay);
                        mycom.Parameters.AddWithValue("@SEQ_ISO_SPD", this.Sequence.ISOSpeed);
                        mycom.Parameters.AddWithValue("@SEQ_BLB_MOD", this.Sequence.BulbMode);
                        System.Diagnostics.Debug.WriteLine(Sequence);
                        mycom.ExecuteNonQuery();

                        // get auto-generated primary key of last row inserted
                        mycom.CommandText = "SELECT last_insert_rowid()";
                        tmp_uid = (Int64)mycom.ExecuteScalar();
                        this.Sequence.UID = (int)tmp_uid;

                        sql = @"INSERT INTO Session (SES_UID, SES_ACQ_UID, SES_LOC_UID, SES_TAR_UID, SES_SEQ_UID)
                                VALUES (NULL, @SES_ACQ_UID, @SES_LOC_UID, @SES_TAR_UID, @SES_SEQ_UID);";

                        mycom.CommandText = sql;
                        mycom.Parameters.Clear();
                        mycom.Parameters.AddWithValue("@SES_ACQ_UID", this.Acquisition.UID);
                        mycom.Parameters.AddWithValue("@SES_LOC_UID", this.Location.UID);
                        mycom.Parameters.AddWithValue("@SES_TAR_UID", this.Target.UID);
                        mycom.Parameters.AddWithValue("@SES_SEQ_UID", this.Sequence.UID);

                        mycom.ExecuteNonQuery();

                        mycom.CommandText = "SELECT last_insert_rowid()";
                        tmp_uid = (Int64)mycom.ExecuteScalar();
                        this.UID = (int)tmp_uid;
                    }
                }

                mytran.Commit();
            }

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        private bool SelectAcquisition(string ID)
        {
            DataTable dt = new DataTable();

            try
            {
                SQLiteCommand mycom = new SQLiteCommand(dbconn);
                mycom.CommandText = @"SELECT * FROM Acquisition WHERE ACQ_UID=@ID;";
                mycom.Parameters.AddWithValue("@ID", ID);

                SQLiteDataReader rdr = mycom.ExecuteReader();
                rdr.Read();
                this.Acquisition.Name = rdr[1].ToString();
                this.Acquisition.Date = Convert.ToDateTime(rdr[2].ToString());
                this.Acquisition.Time = Convert.ToDateTime(rdr[3].ToString());
                this.Acquisition.Temperature = Convert.ToInt32(rdr[4].ToString());

                //dt.Load(rdr);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        private bool SelectLocation(string ID)
        {
            DataTable dt = new DataTable();

            //try
            //{
                SQLiteCommand mycom = new SQLiteCommand(dbconn);
                mycom.CommandText = @"SELECT * FROM Location WHERE LOC_UID=@ID;";
                mycom.Parameters.AddWithValue("@ID", ID);

                SQLiteDataReader rdr = mycom.ExecuteReader();
                rdr.Read();
                this.Location.Name = rdr[1].ToString();
                this.Location.LongitudeDegrees = Convert.ToInt32(rdr[2].ToString());
                this.Location.LongitudeMinutes = Convert.ToInt32( rdr[3].ToString());
                this.Location.LongitudeSeconds = Convert.ToDecimal(rdr[4].ToString());
                this.Location.LatitudeDegrees = Convert.ToInt32(rdr[6].ToString());
                this.Location.LatitudeMinutes = Convert.ToInt32(rdr[7].ToString());
                this.Location.LatitudeSeconds = Convert.ToDecimal(rdr[8].ToString());
                //dt.Load(rdr);
            //}
            //catch (Exception e)
            //{
            //    throw new Exception(e.Message);
            //}

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public bool Delete(string ID)
        {
            string acq_uid = "";
            string loc_uid = "";
            string tar_uid = "";
            string seq_uid = "";

            bool status = true;

            using (SQLiteCommand mycom = new SQLiteCommand(dbconn))
            {
                string sql = @"SELECT * FROM Session WHERE SES_UID=@SES_UID;";

                mycom.CommandText = sql;
                mycom.Parameters.AddWithValue("@SES_UID", ID);

                SQLiteDataReader rdr = mycom.ExecuteReader();

                if (rdr.Read())
                {
                    acq_uid = rdr[1].ToString();
                    loc_uid = rdr[2].ToString();
                    tar_uid = rdr[3].ToString();
                    seq_uid = rdr[4].ToString();
                }
            }

            using (SQLiteTransaction mytran = dbconn.BeginTransaction())
            {
                using (SQLiteCommand mycom = new SQLiteCommand(dbconn))
                { 
                    string sql = @"DELETE FROM Session WHERE SES_UID=@SES_UID;";
                    mycom.CommandText = sql;
                    mycom.Parameters.AddWithValue("@SES_UID", ID);
                    mycom.ExecuteNonQuery();
                    mycom.Parameters.Clear();

                    sql = @"DELETE FROM Acquisition WHERE ACQ_UID=@ACQ_UID;";
                    mycom.CommandText = sql;
                    mycom.Parameters.AddWithValue("@ACQ_UID", acq_uid);
                    mycom.ExecuteNonQuery();
                    mycom.Parameters.Clear();

                    sql = @"DELETE FROM Location WHERE LOC_UID=@LOC_UID;";
                    mycom.CommandText = sql;
                    mycom.Parameters.AddWithValue("@LOC_UID", loc_uid);
                    mycom.ExecuteNonQuery();
                    mycom.Parameters.Clear();

                    sql = @"DELETE FROM Target WHERE TAR_UID=@TAR_UID;";
                    mycom.CommandText = sql;
                    mycom.Parameters.AddWithValue("@TAR_UID", tar_uid);
                    mycom.ExecuteNonQuery();
                    mycom.Parameters.Clear();

                    sql = @"DELETE FROM Sequence WHERE SEQ_UID=@SEQ_UID;";
                    mycom.CommandText = sql;
                    mycom.Parameters.AddWithValue("@SEQ_UID", seq_uid);
                    mycom.ExecuteNonQuery();
                    mycom.Parameters.Clear();
                }

                mytran.Commit();
            }

            return status;
        }

        public int UID { get; set; }
    }
}
